package com.cx.controller.back.api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cx.entity.ArticleComment;
import com.cx.service.IArticleCommentService;
import com.cx.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@CrossOrigin    //解决跨域问题注解
@RestController
@RequestMapping("/back-article-comment")
public class BackArticleCommentController {
    @Autowired
    private IArticleCommentService articleCommentService;

    //        注意：lay-ui成功code为0
//        查询全部文章评论信息
    @RequestMapping("list")
    public R getArticleCommentAll(@RequestParam Map<String, Object> params) {
        IPage<ArticleComment> articleCommentIPage = articleCommentService.getArticleCommentListPageConditions(params);
        return R.ok(0, "文章评论数据请求成功").put("count", articleCommentIPage.getTotal()).put("data", articleCommentIPage.getRecords());
    }

    //    查询全部评论(递归查询)
    @RequestMapping("articleIdCommentPidAll")
    public R findCommentTree(Integer articleId) {
//        从根（pid最小=0）开始查询评论
        return R.ok().put("data", articleCommentService.findarticleIdCommentList(articleId, 0));
    }


    //    文章评论信息添加
    @RequestMapping("save")
    public R articleCommentServiceSave(ArticleComment articleComment) {
        return R.ok().put("data", articleCommentService.save(articleComment));
    }

    //    文章评论修改
    @RequestMapping("update")
    public R articleCommentServiceUpdateById(ArticleComment articleComment) {
        return R.ok().put("data", articleCommentService.updateById(articleComment));
    }

    //    文章评论删除
    @RequestMapping("del")
    public R delete(@RequestBody Integer[] ids) {//[1,2]
        articleCommentService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
